<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 主要内容 -->
    <div class="login-container">
      <!-- 左侧信息面板 -->
      <div class="info-panel">
        <div class="brand-section">
          <div class="brand-icon">
            <el-icon size="48"><UserFilled /></el-icon>
          </div>
          <h1 class="brand-title">暖星心里守护站</h1>
          <p class="brand-subtitle">医生专用密码登录</p>
        </div>
        
        <div class="features-section">
          <div class="feature-item">
            <el-icon class="feature-icon"><Lock /></el-icon>
            <div class="feature-content">
              <h3>安全登录</h3>
              <p>使用您的医生账号和密码安全登录系统</p>
            </div>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><Document /></el-icon>
            <div class="feature-content">
              <h3>专业管理</h3>
              <p>访问您的医生工作台，管理患者信息和预约</p>
            </div>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><Setting /></el-icon>
            <div class="feature-content">
              <h3>个人设置</h3>
              <p>管理您的个人信息和专业资料</p>
            </div>
          </div>
        </div>
        
        <div class="bottom-info">
          <p>返回<router-link to="/login" class="link">人脸识别登录</router-link></p>
          <p>没有账户？<router-link to="/doctor/register" class="link">医生注册</router-link></p>
        </div>
      </div>
      
      <!-- 右侧登录面板 -->
      <div class="login-panel">
        <div class="login-content">
          <!-- 登录表单 -->
          <div class="login-form">
            <div class="login-header">
              <h2 class="login-title">医生登录</h2>
              <p class="login-subtitle">请输入您的医生账号和密码</p>
            </div>
            
            <el-form 
              ref="loginFormRef" 
              :model="loginForm" 
              :rules="loginRules" 
              class="doctor-login-form"
            >
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  placeholder="请输入医生工号或用户名"
                  size="large"
                  :prefix-icon="User"
                  class="login-input"
                />
              </el-form-item>
              
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  placeholder="请输入密码"
                  size="large"
                  :prefix-icon="Lock"
                  show-password
                  class="login-input"
                  @keyup.enter="handleLogin"
                />
              </el-form-item>
              
              <el-form-item>
                <el-button
                  type="primary"
                  size="large"
                  @click="handleLogin"
                  :loading="logging"
                  class="login-button"
                >
                  <el-icon v-if="!logging"><Unlock /></el-icon>
                  {{ logging ? '登录中...' : '登录' }}
                </el-button>
              </el-form-item>
            </el-form>
            
            <!-- 其他选项 -->
            <div class="other-options">
              <div class="forgot-password">
                <a href="#" class="link-text">忘记密码？</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { UserFilled, User, Lock, Document, Setting, Unlock } from '@element-plus/icons-vue'
import { doctorLogin } from '../../api/doctor.js'

const router = useRouter()
const loginFormRef = ref(null)
const logging = ref(false)

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入医生工号或用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return
    
    logging.value = true
    
    const response = await doctorLogin({
      doctorName: loginForm.username,
      password: loginForm.password
    })
    
    if (response.result) {
      ElMessage.success(response.msg || '登录成功')
      
      // 存储登录信息到 localStorage
      localStorage.setItem('doctorInfo', JSON.stringify(response.data))
      
      // 跳转到首页
      router.push('/home')
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
    
  } catch (error) {
    console.error('Login error:', error)
    ElMessage.error(error.message || '登录失败，请检查网络连接')
  } finally {
    logging.value = false
  }
}
</script>

<style scoped>
.login-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: 
    linear-gradient(135deg, 
      rgba(165, 214, 167, 0.9) 0%,
      rgba(129, 199, 132, 0.8) 25%,
      rgba(79, 195, 247, 0.8) 75%,
      rgba(144, 202, 249, 0.9) 100%
    ),
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 80"><defs><pattern id="leaves" width="80" height="80" patternUnits="userSpaceOnUse"><g opacity="0.1"><path d="M20,40 Q25,20 40,25 Q55,30 50,45 Q45,60 30,55 Q15,50 20,40" fill="white"/><path d="M60,20 Q65,0 80,5 Q95,10 90,25 Q85,40 70,35 Q55,30 60,20" fill="white"/><circle cx="10" cy="70" r="3" fill="white" opacity="0.3"/><circle cx="70" cy="60" r="2" fill="white" opacity="0.2"/></g></pattern></defs><rect width="100%" height="100%" fill="url(%23leaves)"/></svg>'),
    radial-gradient(circle at 20% 30%, rgba(255,255,255,0.15) 0%, transparent 40%),
    radial-gradient(circle at 80% 70%, rgba(129, 199, 132,0.2) 0%, transparent 50%),
    radial-gradient(circle at 60% 20%, rgba(79, 195, 247,0.15) 0%, transparent 45%),
    linear-gradient(45deg, #e8f5e8 0%, #e3f2fd 100%);
  background-size: 
    100% 100%,
    80px 80px,
    300px 300px,
    400px 400px,
    350px 350px,
    100% 100%;
  background-repeat: no-repeat;
  background-position: center center;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: auto;
  overflow-y: auto;
  z-index: 0;
}

.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.15) 0%, 
    rgba(129, 199, 132, 0.1) 50%, 
    rgba(79, 195, 247, 0.1) 100%
  );
  backdrop-filter: blur(25px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: healingFloat 8s ease-in-out infinite;
  box-shadow: 
    0 8px 32px rgba(129, 199, 132, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

.circle-1 {
  width: 180px;
  height: 180px;
  top: 8%;
  left: 8%;
  animation-delay: 0s;
}

.circle-2 {
  width: 120px;
  height: 120px;
  top: 55%;
  right: 12%;
  animation-delay: 2.5s;
}

.circle-3 {
  width: 90px;
  height: 90px;
  bottom: 15%;
  left: 18%;
  animation-delay: 5s;
}

.circle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 60%;
  height: 60%;
  border-radius: 50%;
  background: radial-gradient(circle, 
    rgba(255, 255, 255, 0.3) 0%, 
    transparent 70%
  );
  transform: translate(-50%, -50%);
}

@keyframes healingFloat {
  0%, 100% { 
    transform: translateY(0px) rotate(0deg) scale(1); 
    opacity: 0.6;
  }
  33% { 
    transform: translateY(-15px) rotate(120deg) scale(1.05); 
    opacity: 0.8;
  }
  66% { 
    transform: translateY(-8px) rotate(240deg) scale(0.95); 
    opacity: 0.7;
  }
}

.login-container {
  position: relative;
  z-index: 2;
  display: flex;
  width: min(1200px, 95vw);
  min-width: 300px;
  height: min(700px, 90vh);
  min-height: 500px;
  background: 
    linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(248, 252, 248, 0.95) 100%);
  border-radius: 24px;
  box-shadow: 
    0 25px 50px rgba(0, 0, 0, 0.12),
    0 0 0 1px rgba(129, 199, 132, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(25px);
  overflow: hidden;
  margin: 20px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.info-panel {
  flex: 1;
  background: linear-gradient(135deg, #81C784 0%, #4FC3F7 100%);
  color: white;
  padding: 60px 50px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

.info-panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

.brand-section {
  position: relative;
  z-index: 1;
  text-align: center;
}

.brand-icon {
  margin-bottom: 20px;
}

.brand-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 10px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin: 0;
}

.features-section {
  position: relative;
  z-index: 1;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 30px;
}

.feature-icon {
  margin-right: 20px;
  margin-top: 5px;
  font-size: 24px;
  opacity: 0.9;
}

.feature-content h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.feature-content p {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
  line-height: 1.5;
}

.bottom-info {
  position: relative;
  z-index: 1;
  text-align: center;
  font-size: 16px;
  opacity: 0.9;
}

.link {
  color: white;
  text-decoration: none;
  font-weight: 600;
  border-bottom: 1px solid rgba(255, 255, 255, 0.5);
  transition: all 0.3s ease;
}

.link:hover {
  border-bottom-color: white;
}

.login-panel {
  flex: 1;
  padding: 60px 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-content {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin: 0 0 10px 0;
}

.login-subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.doctor-login-form {
  margin-top: 30px;
}

.login-input {
  margin-bottom: 20px;
  border-radius: 12px;
}

.login-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(129, 199, 132, 0.2);
  transition: all 0.3s ease;
}

.login-input :deep(.el-input__wrapper:hover) {
  border-color: rgba(129, 199, 132, 0.4);
}

.login-input :deep(.el-input__wrapper.is-focus) {
  border-color: #81C784;
  box-shadow: 0 0 0 2px rgba(129, 199, 132, 0.2);
}

.login-button {
  width: 100%;
  height: 50px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #81C784 0%, #4FC3F7 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(129, 199, 132, 0.4);
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(129, 199, 132, 0.6);
}

.other-options {
  margin-top: 30px;
  text-align: center;
}

.link-text {
  color: #81C784;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.link-text:hover {
  color: #4FC3F7;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-container {
    flex-direction: column;
    width: 500px;
    height: auto;
  }
  
  .info-panel {
    padding: 40px 35px 20px 35px;
  }
  
  .features-section {
    display: none;
  }
  
  .login-panel {
    padding: 20px 35px 40px 35px;
  }
}

@media (max-width: 768px) {
  .login-container {
    width: 95%;
    margin: 20px;
    border-radius: 16px;
  }
  
  .info-panel, .login-panel {
    padding: 30px 25px;
  }
  
  .login-content {
    max-width: 100%;
  }
}

@media (max-width: 480px) {
  .login-page {
    padding: 10px;
  }
  
  .login-container {
    width: 100%;
    min-width: 280px;
    margin: 10px;
    border-radius: 16px;
  }
  
  .info-panel, .login-panel {
    padding: 20px 15px;
  }
  
  .brand-title {
    font-size: 22px;
  }
  
  .login-title {
    font-size: 20px;
  }
}
</style>
